﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Collections.Generic;
using System.Globalization;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Microsoft.Practices.ObjectBuilder;
using Microsoft.Practices.Web.UI.WebControls;
using Microsoft.Practices.CompositeWeb.Web;
using Microsoft.Practices.CompositeWeb;
using WCSFContrib.ECommerceCatalog.Product.Views;
using WCSFContrib.ECommerceCatalog.BusinessEntities;

namespace WCSFContrib.ECommerceCatalog.Product.Views
{

    public partial class Product_Default : Microsoft.Practices.CompositeWeb.Web.UI.Page, IDefaultView
    {
        private DefaultPresenter _presenter;
        private const string ShowProductDetailsCommand = "ShowProductDetails";

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!this.IsPostBack)
            {
                this._presenter.OnViewInitialized();

                SearchTextBox.Focus();
            }
            this._presenter.OnViewLoaded();
        }

        [CreateNew]
        public DefaultPresenter Presenter
        {
            set
            {
                this._presenter = value;
                this._presenter.View = this;
            }
        }

        #region IDefaultView Members

        public void ShowProducts(ICollection<Item> items)
        {
            ProductsContainerDataSource.DataSource = items;
        }

        public void ShowProductDetails(Item item)
        {
            ProductDetailsUserControl.ShowProductDetails(item);

            DetailsModalPopupExtender.Show();
        }

        public int TotalProductsCount
        {
            set
            {
                ProductsContainerDataSource.TotalRowCount = value;

                ViewState["TotalOrdersCount"] = value;
            }
            get
            {
                return (int)(ViewState["TotalOrdersCount"] ?? 0);
            }
        }

        public int MaximumRowsPerPage
        {
            get { return ProductsGridView.PageSize; }
        }

        public int PageCount
        {
            get
            {
                if (TotalProductsCount == 0)
                    return 0;

                return (TotalProductsCount / MaximumRowsPerPage) + 1;
            }
        }

        public void ShowCurrentPage(int currentPage)
        {
            ProductsGridView.PageIndex = currentPage;
        }

        #endregion

        //protected void SearchButton_Click(object sender, EventArgs e)
        //{
        //    _presenter.OnSearchTextChanged();
        //}

        protected void SearchTextBox_OnTextChanged(object sender, EventArgs e)
        {
            _presenter.OnSearchTextChanged();
        }

        protected void ProductsGridView_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName == ShowProductDetailsCommand)
            {
                int rowIndex = int.Parse((string)e.CommandArgument, CultureInfo.CurrentCulture);
                int productId = Convert.ToInt32(((GridView)e.CommandSource).DataKeys[rowIndex].Value);
                _presenter.OnProductDetailsRequested(productId);

                QuantityNumericUpDownExtender.Maximum = ProductDetailsUserControl.ItemQuantity;
                if (ProductDetailsUserControl.ItemQuantity <= 0)
                {
                    QuantityNumericUpDownExtender.Minimum = 0;
                    QuantityTextBox.Text = "0";
                    AddCartButton.Enabled = false;
                }
                else
                {
                    QuantityNumericUpDownExtender.Minimum = 1;
                    QuantityTextBox.Text = "1";
                    AddCartButton.Enabled = true;
                }
            }
        }

        protected void ProductsContainerDataSource_Selecting(object sender, ObjectContainerDataSourceSelectingEventArgs e)
        {
            string searchText = SearchTextBox.Text;
            _presenter.OnProductsSelecting(searchText);
        }

        protected void AddCartButton_Click(object sender, EventArgs e)
        {
            string userName = User.Identity.Name;
            int itemId = ProductDetailsUserControl.ItemId;
            int quantity = int.Parse(QuantityTextBox.Text, CultureInfo.CurrentCulture);

            _presenter.OnAddProductToCart(userName, itemId, quantity);
        }
    }
}